package com.alittle.module.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alittle.R;
import com.alittle.core.ALBaseMvpActivity;
import com.alittle.core.LittleRouter;
import com.alittle.core.view.ModalIndicator;
import com.alittle.mvp.mine.ILoginView;

import core.DataError;
import dev.xesam.androidkit.utils.ToastUtil;

/**
 * Created by wom on 2015/8/18.
 */
public class LoginActivity extends ALBaseMvpActivity<LoginPresenter> implements ILoginView, View.OnClickListener {

    private final static int APP_VERIFY_CODE_TIME_LIMIT = 120000;

    private EditText phoneEt, verifyEt;
    private Button verifyBtn, sureBtn;

    private CountDownTimer timer;
    private boolean isTiming = false;  //当前是否在计时

    private ModalIndicator vLogging;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parents_login);

        verifyBtn = (Button) findViewById(R.id.parernts_user_confirm_btn);
        sureBtn = (Button) findViewById(R.id.parents_user_make_sure_btn);
        phoneEt = (EditText) findViewById(R.id.parents_user_phone_num_edt);
        verifyEt = (EditText) findViewById(R.id.parentsr_user_confirm_num_edt);
        initView();
        addListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void showPhoneLegal() {

        if (!isTiming) {
            verifyBtn.setText("验证");
            verifyBtn.setEnabled(true);
            verifyEt.setEnabled(true);
        }
    }

    @Override
    public void showPhoneIllegal() {

        if (!isTiming) {
            verifyBtn.setText("验证");
            verifyBtn.setEnabled(false);
        }
    }

    @Override
    public void enableLogin() {
        sureBtn.setEnabled(true);
    }

    @Override
    public void disableLogin() {
        sureBtn.setEnabled(false);
    }

    @Override
    public void showSendVerifyCodeSuccess() {
        ToastUtil.showToast(this, "验证码已发送");
    }

    @Override
    public void showLogging() {
        vLogging = new ModalIndicator(this);
        vLogging.setIndicateMessage("正在登陆").show();
    }

    @Override
    public void showLoginError(DataError error) {
        vLogging.dismiss();
//        ToastUtil.showToast(this, error.message);TODO 网络后台逻辑
        verifyEt.setText(null);
        isTiming = false;
    }

    @Override
    public void showLoginSuccess(String phone) {
        vLogging.dismiss();
        LittleRouter.routeToPanelHost(this);
        finish();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.parernts_user_confirm_btn) {
            timer.start(); //点击就开始计时
            verifyEt.requestFocus();
            presenter.requestVerifyCode(phoneEt.getText().toString());
        } else if (id == R.id.parents_user_make_sure_btn) {
            presenter.login(phoneEt.getText().toString(), verifyEt.getText().toString());
        }
    }


    /*#############################################PRIVATE##########################################*/

    private void initView() {

        timer = new CountDownTimer(APP_VERIFY_CODE_TIME_LIMIT, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                isTiming = true;
                verifyBtn.setText((millisUntilFinished / 1000) + "秒");
                verifyBtn.setEnabled(false);
            }

            @Override
            public void onFinish() {
                isTiming = false;
//                presenter.verifyInvaild();//超出验证时间，检查验证失败的原因
                //Todo 逻辑还得想
            }
        };
    }

    private void addListener() {

        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                presenter.verifyInvaild(LoginPresenter.TYPE_PHONE, phoneEt.getText().toString());
            }
        });

        verifyEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                presenter.verifyInvaild(LoginPresenter.TYPE_VERIFY_CODE, verifyEt.getText().toString());
            }
        });
        verifyBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

}
